package com.test;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1 {
    public static void main(String[] args) {
        String queueName = "queue_name"; // 替换为你的队列名称

        // 创建ConnectionFactory实例
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.159.142");
//        factory.setPort(5672);
//        factory.setUsername("guest");
//        factory.setPassword("guest");

        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            // 声明队列
            channel.queueDeclare(queueName, true, false, false, null);

            // 创建消费者
            DefaultConsumer consumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("consumerTag=" + consumerTag);
                    // 信封里，封装的相关信息：
                    System.out.println("路由key:"+envelope.getRoutingKey());
                    System.out.println("交换机:"+envelope.getExchange());
                    System.out.println("消息id:"+envelope.getDeliveryTag());
                    // 消息内容
                    System.out.println("消息内容:" + new String(body, "UTF-8"));
                }
            };
            channel.basicConsume(Producer.QUEUE_NAME1,true,consumer);
        } catch (Exception e) {
            e.printStackTrace();
        }


//        //1,得到创建工厂
//        ConnectionFactory factory = new ConnectionFactory();
//        factory.setHost("192.168.159.142");
//        factory.setPort(5672);
//        factory.setUsername("guest");
//        factory.setPassword("guest");
//
//        // java8新的try..catch语法，这种语法的好处是不用我们手动的关闭connection
//        try (Connection connection = factory.newConnection();
//             //2,得到连接
//             Channel channel = connection.createChannel()) {
//            //3,申明交换机
//            channel.exchangeDeclare(Producer.EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
//            //4,申明队列
//            channel.queueDeclare(Producer.QUEUE_NAME1, true, false, false, null);
////            channel.queueDeclare(Producer.QUEUE_NAME2, true, false, false, null);
//            //把队列绑定到交换机上
//            channel.queueBind(Producer.QUEUE_NAME1, Producer.EXCHANGE_NAME, "");
////            channel.queueBind(Producer.QUEUE_NAME2, Producer.EXCHANGE_NAME, "");
//            //5,创建一个消费者
//            DefaultConsumer consumer = new DefaultConsumer(channel) {
//                @Override
//                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                    System.out.println("consumerTag=" + consumerTag);
//                    // 信封里，封装的相关信息：
//                    System.out.println("路由key:"+envelope.getRoutingKey());
//                    System.out.println("交换机:"+envelope.getExchange());
//                    System.out.println("消息id:"+envelope.getDeliveryTag());
//                    // 消息内容
//                    System.out.println("消息内容:" + new String(body, "UTF-8"));
//                }
//            };
//            channel.basicConsume(Producer.QUEUE_NAME1,true,consumer);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}
